<?

/*---------------------------------------------------+
| mysqldump.php
+----------------------------------------------------+
| Copyright 2006 Huang Kai
| hkai@atutility.com
| http://atutility.com/
+----------------------------------------------------+
| Released under the terms & conditions of v2 of the
| GNU General Public License. For details refer to
| the included gpl.txt file or visit http://gnu.org
+----------------------------------------------------*/
/*
change log:
2006-10-16 Huang Kai
---------------------------------
initial release

2006-10-18 Huang Kai
---------------------------------
fixed bugs with delimiter
add paramter header to add field name as CSV file header.

2006-11-11 Huang Kia
Tested with IE and fixed the <button> to <input>


2012-02-08 Edited by Laurens Laman
*/
include('../../src/inst.conf.php');

$mysqldump_version="1.02";

$mysql_host		=	$cfg['db']['host'];
$mysql_database	=	$cfg['db']['database'];
$mysql_username	=	$cfg['db']['user'];
$mysql_password	=	$cfg['db']['password'];


//ob_start("ob_gzhandler");
if(isset($_GET['file'])){
	header('Content-type: text/plain');
	header('Content-Disposition: attachment; filename="'.$mysql_host."_".$mysql_database."_".date('Y-m-d H_i_s').'.sql"');
}else{
echo "<pre>";
}
echo "/* 
BACKUP ".date('Y-m-d H:i:s')."
DOWNLOADED FROM: http://".$_SERVER['HTTP_HOST']."/
HOST: ".$mysql_host."
DATABASE: ".$mysql_database."

mysqldump.php version @$mysqldump_version 

*/\n\n\n\n";
_mysqldump($mysql_database);






function _mysqldump($mysql_database)
{
	$sql="show tables;";
	$result= mysql_query($sql);
	if( @$result)
	{
		while( @$row= mysql_fetch_row($result))
		{
			_mysqldump_table_structure($row[0]);
			_mysqldump_table_data($row[0]);
		}
	}
	else
	{
		echo "/* no tables in @$mysql_database */\n";
	}
	mysql_free_result($result);
}

function _mysqldump_table_structure($table)
{
	echo "/* Table structure for table `$table` */\n";
	echo "DROP TABLE IF EXISTS `$table`;\n\n";
	$sql="show create table `$table`; ";
	$result=mysql_query($sql);
	if( @$result)
	{
		if($row= mysql_fetch_assoc($result))
		{
			echo @$row['Create Table'].";\n\n";
		}
	}
	mysql_free_result($result);
}

function _mysqldump_table_data($table)
{

	$sql="select * from `$table`;";
	$result=mysql_query($sql);
	if( @$result)
	{
		$num_rows= mysql_num_rows($result);
		$num_fields= mysql_num_fields($result);

		if( @$num_rows > 0)
		{
			echo "/* dumping data for table `$table` */\n";

			$field_type=array();
			$i=0;
			while( @$i < @$num_fields)
			{
				$meta= mysql_fetch_field($result, @$i);
				array_push($field_type, @$meta->type);
				$i++;
			}

			//print_r( @$field_type);
			echo "insert into `$table` values\n";
			$index=0;
			while( @$row= mysql_fetch_row($result))
			{
				echo "(";
				for( @$i=0; @$i < @$num_fields; @$i++)
				{
					if( is_null( @$row[$i]))
						echo "null";
					else
					{
						switch( @$field_type[$i])
						{
							case 'int':
								echo @$row[$i];
								break;
							case 'string':
							case 'blob' :
							default:
								echo "'".mysql_real_escape_string($row[$i])."'";

						}
					}
					if( @$i < @$num_fields-1)
						echo ",";
				}
				echo ")";

				if( @$index < @$num_rows-1)
					echo ",";
				else
					echo ";";
				echo "\n";

				$index++;
			}
		}
	}
	mysql_free_result($result);
	echo "\n";
}


?>